<template>
	<div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
// require('echarts/theme/macarons'); // echarts theme
import resize from "./mixins/resize";
var splitPointer = 0;
const EraseWindow = 200;
let timer = null;
export default {
	mixins: [resize],
	props: {
		className: {
			type: String,
			default: "chart"
		},
		width: {
			type: String,
			default: "100%"
		},
		height: {
			type: String,
			default: "100%"
		},
		autoResize: {
			type: Boolean,
			default: true
		},
		chartData: {
			type: Object,
			required: false
		}
	},
	data() {
		return {
			chart: null,
			ecg: [],
			prevEcg: []
		};
	},
	watch: {
		chartData: {
			deep: true,
			handler(val) {
				this.setOptions(val);
			}
		}
	},
	mounted() {
		this.$nextTick(() => {
			this.chart = echarts.init(this.$el);
			this.ecg = [];
			this.prevEcg = [];
			setTimeout(() => {
				this.resize();
			}, 350);
		});
	},
	beforeDestroy() {
		if (!this.chart) {
			return;
		}
		this.chart.dispose();
		this.chart = null;

		if (timer) {
			clearInterval(timer);
			timer = null;
		}
	},
	methods: {
		setOptions({ expectedData, actualData } = {}) {
			this.chart &&
				this.chart.setOption({
					xAxis: [
						{
							// type: "value",
							boundaryGap: false,
							// data: Array.from(
							// 	{
							// 		length: 1000
							// 	},
							// 	(_, i) => i
							// ),
							data: Array.from(
								{
									length: 3100
								},
								(_, i) => i
							).filter((_, index) => index % 300 === 0),

							//boundaryGap: [0, "100%"],
							axisTick: {
								show: false // 不显示坐标轴线
							},
							axisLine: {
								show: false // 不显示坐标轴线
							},
							axisLabel: {
								show: true, // 不显示坐标轴上的文字
								formatter: function (value, index) {
									return parseFloat((value / 500).toFixed(1)) + "s";
								}
							},
							splitLine: {
								show: true, // 不显示网格线
								lineStyle: {
									color: "rgba(255,255,255,.2)", // 坐标轴线线的颜色
									width: "1", // 坐标轴线线宽
									type: "solid" // 坐标轴线线的类型（solid实线类型；dashed虚线类型；dotted点状类型）
								}
							}
							// splitNumber: 20,
							// interval: 100
						},
						{
							// type: "value",
							boundaryGap: false,
							data: Array.from(
								{
									length: 3000
								},
								(_, i) => i
							),
							//data: Array.from({ length: 1000 }, (_, i) => i).filter((_, index) => index % 100 === 0),

							//boundaryGap: [0, "100%"],
							axisTick: {
								show: false // 不显示坐标轴线
							},
							axisLine: {
								show: false // 不显示坐标轴线
							},
							axisLabel: {
								show: false // 不显示坐标轴上的文字
							},
							splitLine: {
								show: false, // 不显示网格线
								lineStyle: {
									color: "rgba(255,255,255,.2)", // 坐标轴线线的颜色
									width: "1", // 坐标轴线线宽
									type: "solid" // 坐标轴线线的类型（solid实线类型；dashed虚线类型；dotted点状类型）
								}
							}
							// splitNumber: 10,
							// interval: 100
						}
					],
					yAxis: {
						type: "value",
						boundaryGap: [0, "100%"],
						axisLine: {
							show: false // 不显示坐标轴线
						},
						axisLabel: {
							show: true, // 不显示坐标轴上的文字
							formatter: function (value, index) {
								return value == 1 ? "1mV" : value;
							}
						},
						splitLine: {
							show: true, // 不显示网格线
							lineStyle: {
								color: "rgba(255,255,255,.2)", // 坐标轴线线的颜色
								width: "1", // 坐标轴线线宽
								type: "solid" // 坐标轴线线的类型（solid实线类型；dashed虚线类型；dotted点状类型）
							}
						},
						axisTick: {
							show: false // 不显示坐标轴线
						},
						min: -1,
						max: 1,
						// splitNumber: 4,
						interval: 0.5
					},
					grid: {
						left: 10,
						right: 10,
						bottom: 10,
						top: 10,
						containLabel: true
					},
					// tooltip: {
					// 	trigger: 'axis',
					// 	axisPointer: {
					// 		type: 'cross'
					// 	},
					// 	padding: [5, 10]
					// },
					series: [
						{
							name: "expected",
							itemStyle: {
								normal: {
									color: "#01f901",
									width: 1,
									lineStyle: {
										color: "#01f901",
										width: 1
									}
								}
							},

							smooth: true,
							showSymbol: false,
							//sampling: "average", // 采样方法：平均值
							type: "line",
							data: expectedData, //
							animationDuration: 2800,
							animationEasing: "cubicInOut",
							xAxisIndex: 1
						},
						{
							name: "actual",
							smooth: true,
							type: "line",
							//sampling: "average", // 采样方法：平均值
							showSymbol: false,
							itemStyle: {
								normal: {
									color: "#01f901",
									width: 1,
									lineStyle: {
										color: "#01f901",
										width: 1
									}
								}
							},
							data: actualData,
							animationDuration: 2800,
							animationEasing: "quadraticOut",
							xAxisIndex: 1
						}
					]
				});
		}
		// resize() {
		// 	console.log('这里触发了吗')
		// 	this.chart && this.chart.resize();
		// }
	}
};
</script>
